<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form
          :inline="true"
          :model="pointsDividendTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('date')" prop="date">
            <el-date-picker
              v-model="pointsDividendTable.searchParam.date"
              type="date"
              value-format="YYYY-MM-DD"
              :placeholder="t('datePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('turnoverType')" prop="turnover_type">
            <el-select v-model="pointsDividendTable.searchParam.turnover_type">
              <el-option
                v-for="(item, index) in turnoverTypeList"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="t('status')" prop="status">
            <el-select v-model="pointsDividendTable.searchParam.status">
              <el-option
                v-for="(item, index) in executionStatusList"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadPointsDividendList()">{{
              t("search")
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{ t("reset") }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="pointsDividendTable.data"
          size="large"
          v-loading="pointsDividendTable.loading"
        >
          <template #empty>
            <span>{{ !pointsDividendTable.loading ? t("emptyData") : "" }}</span>
          </template>
          <el-table-column
            type="index"
            :label="t('serialNumber')"
            min-width="80"
            align="center"
          />
          <el-table-column
            prop="date"
            :label="t('date')"
            min-width="120"
            show-overflow-tooltip
          />

          <el-table-column
            prop="total_turnover"
            :label="t('totalTurnover')"
            min-width="120"
            show-overflow-tooltip
          />

          <el-table-column
            prop="hand_total_turnover"
            :label="t('handTotalTurnover')"
            min-width="120"
            show-overflow-tooltip
          />

          <el-table-column
            prop="total_contribution"
            :label="t('totalContribution')"
            min-width="120"
            show-overflow-tooltip
          />

          <el-table-column :label="t('dividendRate')" min-width="120">
            <template #default="{ row }"> {{ row.dividend_rate || "-" }}% </template>
          </el-table-column>
          <el-table-column :label="t('turnoverType')" min-width="120">
            <template #default="{ row }">
              <Dict-tag :value="row.turnover_type" :list="turnoverTypeList" />
            </template>
          </el-table-column>
          <el-table-column :label="t('status')" min-width="120">
            <template #default="{ row }">
              <Dict-tag :value="row.status" :list="executionStatusList" />
            </template>
          </el-table-column>
          <el-table-column
            :label="t('executeTime')"
            min-width="120"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              {{ row.execute_time || "" }}
            </template>
          </el-table-column>
          <el-table-column :label="t('operation')" fixed="right" min-width="120">
            <template #default="{ row }">
              <el-button type="primary" link @click="editEvent(row)">{{
                t("edit")
              }}</el-button>
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t("delete")
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="pointsDividendTable.page"
            v-model:page-size="pointsDividendTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pointsDividendTable.total"
            @size-change="loadPointsDividendList()"
            @current-change="loadPointsDividendList"
          />
        </div>
      </div>

      <edit ref="editPointsDividendDialog" @complete="loadPointsDividendList" />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { t } from "@/lang";
import { useDictionary } from "@/app/api/dict";
import { getPointsDividendList, deletePointsDividend } from "@/app/api/distributor";
import { ElMessageBox, FormInstance } from "element-plus";
import Edit from "@/app/views/distributor/components/points-dividend-edit.vue";
import { useRoute } from "vue-router";
const route = useRoute();
const pageName = route.meta.title;

const pointsDividendTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    date: "",
    turnover_type: "",
    status: "",
  },
});

const searchFormRef = ref<FormInstance>();
const executionStatusList = ref<any[]>([]); // 执行状态
const turnoverTypeList = ref<any[]>([]); // 交易额来源

onMounted(() => {
  useDictionary("turnover_type").then((res: any) => {
    turnoverTypeList.value = res.data.dictionary;
  });
  useDictionary("execution_status").then((res: any) => {
    executionStatusList.value = res.data.dictionary;
  });
});

/**
 * 获取绿色积分记录列表
 */
const loadPointsDividendList = (page: number = 1) => {
  pointsDividendTable.loading = true;
  pointsDividendTable.page = page;

  getPointsDividendList({
    page: pointsDividendTable.page,
    limit: pointsDividendTable.limit,
    ...pointsDividendTable.searchParam,
  })
    .then((res) => {
      pointsDividendTable.loading = false;
      pointsDividendTable.data = res.data.data;
      pointsDividendTable.total = res.data.total;
    })
    .catch(() => {
      pointsDividendTable.loading = false;
    });
};
loadPointsDividendList();

const editPointsDividendDialog: Record<string, any> | null = ref(null);
/**
 * 编辑绿色积分记录
 * @param data
 */
const editEvent = (data: any) => {
  editPointsDividendDialog.value.setFormData(data);
  editPointsDividendDialog.value.showDialog = true;
};

/**
 * 删除绿色积分记录
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t("pointsDividendDeleteTips"), t("warning"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning",
  }).then(() => {
    deletePointsDividend(id)
      .then(() => {
        loadPointsDividendList();
      })
      .catch(() => {});
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  loadPointsDividendList();
};
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
